package com.macvon.sys;

import android.content.Intent;
import android.util.Log;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import java.io.File;
import java.io.IOException;

public class SysModule extends ReactContextBaseJavaModule {
    private static String MACVON_REBOOT = "ismart.intent.action_reboot";
    private static String MACVON_WEAK = "ismart.intent.action_wakeup";
    private static String MACVON_INSTALL_APP = "ismart.intent.action_installapp";
    private static String MACVON_POWER_OFF = "ismart.intent.action_poweroff";
    private static final String TAG = "SysModule";

    @NonNull
    @Override
    public String getName() {
        return "MacvonSysModule";
    }

    ReactApplicationContext reactContext;

    public SysModule(@NonNull ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
    }

    @ReactMethod
    public void reboot(){
        Log.d(TAG,"reboot");
        Intent intent = new Intent();
        intent.setAction(MACVON_REBOOT);
        reactContext.sendBroadcast(intent);

    }

    @ReactMethod
    public void weakUp(){
        Log.d(TAG,"weakUp");
        Intent intent = new Intent();
        intent.setAction(MACVON_WEAK);
        reactContext.sendBroadcast(intent);
    }

    @ReactMethod
    public void installApp(String apkPath) throws IOException {
        Log.d(TAG,"installApp: " + apkPath);
        File file = new File(apkPath);
        Log.d(TAG,"apkPath exist: " + file.exists());

        Intent intent = new Intent();
        intent.setAction(MACVON_INSTALL_APP);
        intent.putExtra("installpath",file.toURI().getRawPath());
        reactContext.sendBroadcast(intent);
    }

    @ReactMethod
    public void powerOff(){
        Log.d(TAG,"powerOff");
        Intent intent = new Intent();
        intent.setAction(MACVON_POWER_OFF);
        reactContext.sendBroadcast(intent);
    }
}
